package com.sfx.shortLink.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sfx.shortLink.common.convention.result.Result;
import com.sfx.shortLink.common.convention.result.Results;
import com.sfx.shortLink.dto.req.ShortLinkCreateReqDO;
import com.sfx.shortLink.dto.req.ShortLinkPageReqDTO;
import com.sfx.shortLink.dto.req.ShortLinkUpdateReqDTO;
import com.sfx.shortLink.dto.resp.ShortLinkCreateRespDTO;
import com.sfx.shortLink.dto.resp.ShortLinkGroupCountRespDTO;
import com.sfx.shortLink.dto.resp.ShortLinkPageRespDTO;
import com.sfx.shortLink.service.IShortLinkService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class ShortLinkController {

    private final IShortLinkService shortLinkService;

    /**
     * 短链接跳转原始链接
     */
    @GetMapping("/{short-uri}")
    public Result<Void> restoreUrl(@PathVariable("short-uri") String shortUri, HttpServletRequest request, HttpServletResponse response) {
        //shortLinkService.restoreUrl(shortUri, request, response);
        shortLinkService.restoreUrl1(shortUri, request, response);
        return Results.success();
    }

    @PostMapping("/api/short-link/v1/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO requestParam) {
        shortLinkService.updateShortLink(requestParam);
        return Results.success();
    }

    @PostMapping("/api/short-link/v1/create")
    public Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreateReqDO requestParam) {
        return Results.success(shortLinkService.createShortLink1(requestParam));
    }

    @GetMapping("/api/short-link/v1/page")
    public Result<IPage<ShortLinkPageRespDTO>> shortLinkPage(ShortLinkPageReqDTO requestParam) {
        return Results.success(shortLinkService.shortLinkPage(requestParam));
    }
    /**x
     * 获取 短链接分组下 短链接的数量
     *
     * @param requestParam 参数
     * @return 返回
     */
    @GetMapping("/api/short-link/v1/count")
    public Result<List<ShortLinkGroupCountRespDTO>> getGroupShortLinkCount(@RequestParam("requestParam") List<String> requestParam) {
        return Results.success(shortLinkService.getGroupShortLinkCount(requestParam));
    }
}